<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1200" class="enhanced-dialog">
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:message" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">{{ dialogTitle }}</h4>
        <p class="dialog-subtitle">配置消息发送参数和内容</p>
      </div>
    </div>

    <!-- 表单内容 -->
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="180px"
        v-loading="formLoading"
        class="enhanced-form"
      >
        <!-- 基本配置 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:setting" class="section-icon" />
            <span>基本配置</span>
          </div>

          <el-form-item label="发送类型" prop="chatType" class="form-item">
            <el-select
              v-model="formData.chatType"
              placeholder="请选择发送类型"
              clearable
              class="enhanced-select"
            >
              <template #prefix>
                <Icon icon="ep:user" />
              </template>
              <el-option label="普通客户" value="single" />
              <el-option label="客户群" value="group" />
            </el-select>
          </el-form-item>

          <el-form-item
            label="选择客户"
            prop="toUserList"
            v-if="formData.chatType == 'single'"
            class="form-item"
          >
            <div class="selection-container">
              <div class="selected-tags">
                <el-tag
                  v-for="(tag, index) in toUserListLabel"
                  :key="tag"
                  closable
                  class="custom-tag"
                  :disable-transitions="false"
                  @close="handleClose(index)"
                >
                  <Icon icon="ep:user" class="tag-icon" />
                  {{ tag.name }}
                </el-tag>
              </div>
              <el-button type="primary" @click="changCusomer" size="small" plain class="select-btn">
                <Icon icon="ep:plus" class="btn-icon" />
                选择客户
              </el-button>
            </div>
          </el-form-item>

          <el-form-item
            label="选择群"
            prop="toUserList"
            v-if="formData.chatType == 'group'"
            class="form-item"
          >
            <div class="selection-container">
              <div class="selected-tags">
                <el-tag
                  v-for="(tag, index) in toChatListLabel"
                  :key="tag"
                  closable
                  class="custom-tag"
                  :disable-transitions="false"
                  @close="handleCloseGroup(index)"
                >
                  <Icon icon="ep:user-filled" class="tag-icon" />
                  {{ tag.name }}
                </el-tag>
              </div>
              <el-button
                type="primary"
                @click="changChatGroup"
                size="small"
                plain
                class="select-btn"
              >
                <Icon icon="ep:plus" class="btn-icon" />
                选择群
              </el-button>
            </div>
          </el-form-item>

          <el-form-item label="发送人" prop="sender" class="form-item">
            <el-select
              v-model="formData.sender"
              placeholder="请选择发送人"
              clearable
              class="enhanced-select"
            >
              <template #prefix>
                <Icon icon="ep:user" />
              </template>
              <el-option
                v-for="item in userSimpleList"
                :key="item.weUserId"
                :label="item.nickname"
                :value="item.weUserId"
              />
            </el-select>
          </el-form-item>
        </div>

        <!-- 消息内容 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:document" class="section-icon" />
            <span>消息内容</span>
          </div>

          <el-form-item label="发送内容" prop="msgType" class="form-item">
            <el-radio-group
              v-model="formData.msgType"
              @change="clearVal"
              class="content-type-radio"
            >
              <el-radio :label="2" class="radio-item">
                <Icon icon="ep:edit" class="radio-icon" />
                自定义
              </el-radio>
              <el-radio :label="1" class="radio-item">
                <Icon icon="ep:document" class="radio-icon" />
                选择模板
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item v-if="formData.msgType == 1" label="选择模板" required class="form-item">
            <el-select
              v-model="formData.selectValue"
              placeholder="请选择模板"
              @change="setTemplate"
              class="enhanced-select"
            >
              <template #prefix>
                <Icon icon="ep:document" />
              </template>
              <el-option
                v-for="item in msgTemplateSimpleList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="模板内容" class="form-item">
            <el-input
              v-model="formData.content"
              placeholder="请输入内容"
              type="textarea"
              :rows="3"
              class="enhanced-textarea"
            />
          </el-form-item>
        </div>

        <!-- 附件配置 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:paperclip" class="section-icon" />
            <span>附件配置</span>
          </div>

          <el-form-item label="附件类型" class="form-item">
            <div class="attachment-type-selector">
              <div
                class="type-option"
                @click="setAttachmentType(1)"
                :class="{ 'type-active': formData.attachmentType == 1 }"
              >
                <Icon icon="ep:picture" class="type-icon" />
                <span>图片</span>
              </div>
              <div
                class="type-option"
                @click="setAttachmentType(2)"
                :class="{ 'type-active': formData.attachmentType == 2 }"
              >
                <Icon icon="ep:link" class="type-icon" />
                <span>链接</span>
              </div>
            </div>
          </el-form-item>

          <!-- 图片附件 -->
          <el-form-item v-if="formData.attachmentType == 1" label="请选择图片" class="form-item">
            <div class="material-section">
              <div class="material-header">
                <Icon icon="ep:picture" class="material-icon" />
                <span class="material-title">选择图片素材</span>
                <span class="material-count">已选择 {{ formData.imgUrl.length }}/9</span>
              </div>

              <div class="material-gallery">
                <div
                  class="material-item"
                  :class="{ 'material-selected': formData.imgUrl.indexOf(item.wecomUrl) > -1 }"
                  v-for="item in MaterialMap"
                  :key="item.id"
                  @click="changeImage(item.wecomUrl)"
                >
                  <div class="material-image-container">
                    <el-image
                      :src="item.localUrl"
                      fit="cover"
                      class="material-image"
                      loading="lazy"
                    >
                      <template #error>
                        <div class="image-error">
                          <Icon icon="ep:picture" />
                        </div>
                      </template>
                    </el-image>

                    <!-- 选中标识 -->
                    <div
                      v-if="formData.imgUrl.indexOf(item.wecomUrl) > -1"
                      class="selected-overlay"
                    >
                      <Icon icon="ep:check" class="check-icon" />
                    </div>
                  </div>

                  <div class="material-name">{{ item.materialName || '未命名' }}</div>
                </div>
              </div>

              <div class="material-pagination">
                <Pagination
                  :total="totals"
                  v-model:page="limit.pageNo"
                  v-model:limit="limit.pageSize"
                  @pagination="getMaterialMap"
                  layout="prev, pager, next"
                />
              </div>
            </div>
          </el-form-item>

          <!-- 链接附件 -->
          <template v-if="formData.attachmentType == 2">
            <el-form-item label="链接名称" prop="linkTitle" class="form-item">
              <el-input
                v-model.trim="formData.linkTitle"
                placeholder="请输入链接名称"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:link" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="链接描述" prop="linkDesc" class="form-item">
              <el-input
                v-model.trim="formData.linkDesc"
                placeholder="请输入链接描述"
                type="textarea"
                :rows="2"
                class="enhanced-textarea"
              />
            </el-form-item>

            <el-form-item label="链接缩略图" prop="linkPicUrl" class="form-item">
              <div class="upload-container">
                <UploadImgs v-model="formData.linkPicUrl" :limit="1" class="enhanced-upload" />
                <div class="upload-tip">
                  <Icon icon="ep:info-filled" class="tip-icon" />
                  <span>建议上传尺寸为 200x200 的图片</span>
                </div>
              </div>
            </el-form-item>

            <el-form-item label="链接URL" prop="linkUrl" class="form-item">
              <el-input
                v-model.trim="formData.linkUrl"
                placeholder="请输入链接URL"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:link" />
                </template>
              </el-input>
            </el-form-item>
          </template>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="submitForm" type="primary" :disabled="formLoading" class="submit-btn">
          <Icon icon="ep:check" class="btn-icon" />
          确 定
        </el-button>
        <el-button @click="dialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取 消
        </el-button>
      </div>
    </template>
  </Dialog>

  <CustomerDialog ref="customerDialogs" @success="changeCustomers" />
  <ChatgrounpDialog ref="chatgroupDialogs" @success="changeCharGroups" />
</template>

<script setup lang="ts">
import * as MsgSendLogApi from '@/api/wecom/msgsendlog'
import { getSimpleWeUserList } from '@/api/wecom/user/index.ts'
import { getMsgTemplateSimpleList } from '@/api/wecom/msgtemplate/index.ts'
import * as MaterialApi from '@/api/wecom/material'
import CustomerDialog from './customerDialog.vue'
import ChatgrounpDialog from './chatgroupDialog.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const customerDialogs = ref(null)
const chatgroupDialogs = ref(null)
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  msgType: 2,
  chatType: undefined,
  toUserList: undefined,
  sender: undefined,
  selectValue: undefined,
  msgid: undefined,
  content: undefined,

  attachmentType: 0,
  imgUrl: [],
  linkTitle: undefined,
  linkDesc: undefined,
  linkPicUrl: undefined,
  linkUrl: undefined
})
const MaterialMap = ref([])
const limit = ref({
  pageSize: 8,
  pageNo: 1
})
const totals = ref(0)
// 选择客户
const changCusomer = () => {
  customerDialogs.value.openDialog(toUserListLabel.value)
}
// 选择群
const changChatGroup = () => {
  chatgroupDialogs.value.openDialog(toChatListLabel.value)
}
const toUserListLabel = ref([])
const handleClose = (index) => {
  toUserListLabel.value.splice(index, 1)
  formData.value.toUserList.splice(index, 1)
}
// 选择客户
const changeCustomers = (val) => {
  toUserListLabel.value = val
  formData.value.toUserList = val.map((item) => item.externalUserId)
}
const toChatListLabel = ref([])
// 选择群
const changeCharGroups = (val) => {
  toChatListLabel.value = val
  formData.value.toUserList = val.map((item) => item.chatId)
}
const handleCloseGroup = (index) => {
  toChatListLabel.value.splice(index, 1)
  formData.value.toUserList.splice(index, 1)
}
// 素材列表
const getMaterialMap = async () => {
  const { list, total } = await MaterialApi.getMaterialPage(limit.value)
  MaterialMap.value = list
  totals.value = total
}
const formRules = reactive({
  msgType: [
    {
      required: true,
      message: '消息类型：1-普通消息；2-定时任务不能为空',
      trigger: 'change'
    }
  ],
  chatType: [
    {
      required: true,
      message: '发送类型：1-普通客户；2-客户群不能为空',
      trigger: 'change'
    }
  ],
  toUserList: [
    {
      required: true,
      message: '客户ID集合或群ID集合不能为空',
      trigger: 'blur'
    }
  ],
  sender: [
    {
      required: true,
      message: '发送人不能为空',
      trigger: 'blur'
    }
  ],
  selectValue: [
    {
      required: true,
      message: '模板不能为空',
      trigger: 'blur'
    }
  ],
  msgid: [
    {
      required: true,
      message: '原始消息id不能为空',
      trigger: 'blur'
    }
  ],
  linkTitle: [
    {
      required: true,
      message: '链接名称不能为空',
      trigger: 'blur'
    }
  ],
  linkDesc: [
    {
      required: true,
      message: '链接描述不能为空',
      trigger: 'blur'
    }
  ],
  linkPicUrl: [
    {
      required: true,
      message: '链接缩略图不能为空',
      trigger: 'blur'
    }
  ],
  linkUrl: [
    {
      required: true,
      message: '链接URL不能为空',
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

const userSimpleList = ref([])
const msgTemplateSimpleList = ref([])

// 切换类型清空
const clearVal = () => {
  formData.value.attachmentType = 0
  formData.value.imgUrl = []
  formData.value.linkTitle = undefined
  formData.value.linkDesc = undefined
  formData.value.linkPicUrl = undefined
  formData.value.linkUrl = undefined
}
// 选择模板
const setTemplate = () => {
  const item = msgTemplateSimpleList.value.find((item) => item.id == formData.value.selectValue)
  formData.value.attachmentType = item.attachmentType
  formData.value.imgUrl = item.imgUrl
  formData.value.linkTitle = item.linkTitle
  formData.value.linkDesc = item.linkDesc
  formData.value.linkPicUrl = item.linkPicUrl ? item.linkPicUrl.split(',') : undefined
  formData.value.linkUrl = item.linkUrl
}
// 选择类型
const setAttachmentType = (type) => {
  formData.value.imgUrl = []
  formData.value.linkTitle = undefined
  formData.value.linkDesc = undefined
  formData.value.linkPicUrl = undefined
  formData.value.content = undefined

  if (formData.value.attachmentType == type) {
    formData.value.attachmentType = 0
  } else {
    formData.value.attachmentType = type
  }
}
// 选择素材
const changeImage = (url) => {
  if (formData.value.imgUrl.indexOf(url) > -1) {
    formData.value.imgUrl.splice(formData.value.imgUrl.indexOf(url), 1)
  } else {
    if (formData.value.imgUrl.length == 9) {
      ElMessage.warning('超出最多限制9个!')
    } else {
      formData.value.imgUrl.push(url)
    }
  }
}
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  toUserListLabel.value = []
  toChatListLabel.value = []
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  getMaterialMap()
  userSimpleList.value = await getSimpleWeUserList()
  msgTemplateSimpleList.value = await getMsgTemplateSimpleList()
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MsgSendLogApi.getMsgSendLog(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MsgSendLogApi.MsgSendLogVO
    if (formType.value === 'create') {
      await MsgSendLogApi.createMsgSendLog(data)
      message.success(t('common.createSuccess'))
    } else {
      await MsgSendLogApi.updateMsgSendLog(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
watch(
  () => formData.value.chatType,
  (val) => {
    formData.value.toUserList = undefined
    toUserListLabel.value = []
    toChatListLabel.value = []
  },
  { immediate: true, deep: true }
)
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    msgType: 2,
    chatType: undefined,
    toUserList: undefined,
    sender: undefined,
    selectValue: undefined,
    msgid: undefined,
    content: undefined,

    attachmentType: 0,
    imgUrl: [],
    linkTitle: undefined,
    linkDesc: undefined,
    linkPicUrl: undefined,
    linkUrl: undefined
  }
  formRef.value?.resetFields()
}
</script>

<style scoped lang="scss">
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  padding: 24px;
  background: white;
  max-height: 70vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    max-height: 60vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

.enhanced-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }

    @media (max-width: 768px) {
      margin-bottom: 24px;
    }
  }

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid $primary-light;
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;

    @media (max-width: 768px) {
      font-size: 14px;
      margin-bottom: 16px;
      padding-bottom: 8px;
    }
  }

  .section-icon {
    color: $primary-color;
    font-size: 18px;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .form-item {
    margin-bottom: 24px;

    @media (max-width: 768px) {
      margin-bottom: 20px;
    }

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: $text-regular;

      @media (max-width: 768px) {
        width: 100px !important;
        font-size: 13px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 100px !important;
      }
    }
  }
}

/* 表单控件样式 */
.enhanced-select,
.enhanced-input,
.enhanced-textarea {
  :deep(.el-input__wrapper),
  :deep(.el-textarea__inner) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

/* 选择容器 */
.selection-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.selected-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 32px;
  padding: 8px;
  border: 1px solid $border-light;
  border-radius: $border-radius-small;
  background: #fafbff;

  @media (max-width: 768px) {
    gap: 6px;
    padding: 6px;
  }
}

.custom-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: $primary-light;
  border: 1px solid $primary-border;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
  color: $primary-color;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-color;
    color: white;
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
  }

  .tag-icon {
    font-size: 12px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      font-size: 10px;
    }
  }
}

.select-btn {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 4px;
  border: 2px solid $primary-color;
  color: $primary-color;
  border-radius: $border-radius-small;
  padding: 8px 16px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-light;
    transform: translateY(-1px);
  }

  @media (max-width: 768px) {
    padding: 6px 12px;
    font-size: 12px;
  }

  .btn-icon {
    font-size: 12px;
  }
}

/* 内容类型单选框 */
.content-type-radio {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    gap: 16px;
  }

  .radio-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    border: 2px solid $border-light;
    border-radius: $border-radius-small;
    transition: all 0.3s ease;
    cursor: pointer;
    background: white;
    min-width: 120px;

    @media (max-width: 768px) {
      padding: 10px 16px;
      min-width: 100px;
    }

    &:hover {
      border-color: $primary-border;
      background: $primary-light;
    }

    :deep(.el-radio__input.is-checked) {
      .el-radio__inner {
        background: $primary-color;
        border-color: $primary-color;
      }
    }

    .radio-icon {
      font-size: 16px;
      color: $text-secondary;
      transition: all 0.3s ease;

      @media (max-width: 768px) {
        font-size: 14px;
      }
    }

    &:has(.el-radio__input.is-checked) {
      border-color: $primary-color;
      background: $primary-light;

      .radio-icon {
        color: $primary-color;
      }
    }
  }
}

/* 附件类型选择器 */
.attachment-type-selector {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    gap: 12px;
  }
}

.type-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border: 2px solid $border-color;
  border-radius: $border-radius-small;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  font-weight: 500;
  color: $text-regular;
  background: white;
  min-width: 100px;
  justify-content: center;

  @media (max-width: 768px) {
    padding: 10px 16px;
    font-size: 13px;
    min-width: 80px;
  }

  &:hover {
    border-color: $primary-border;
    background: $primary-light;
    color: $primary-color;
  }

  &.type-active {
    background: $primary-color;
    border-color: $primary-color;
    color: white;
    box-shadow: 0 2px 8px rgba(0, 82, 204, 0.3);
  }
}

.type-icon {
  font-size: 16px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

/* 素材选择区域 */
.material-section {
  border: 1px solid $border-light;
  border-radius: $border-radius-small;
  padding: 20px;
  background: #fafbff;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.material-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    margin-bottom: 16px;
    padding-bottom: 8px;
    flex-wrap: wrap;
  }
}

.material-icon {
  color: $primary-color;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.material-title {
  font-size: 16px;
  font-weight: 600;
  color: $text-primary;
  flex: 1;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.material-count {
  font-size: 13px;
  color: $text-secondary;
  background: white;
  padding: 4px 12px;
  border-radius: 12px;
  border: 1px solid $border-light;

  @media (max-width: 768px) {
    font-size: 12px;
    padding: 3px 8px;
  }
}

.material-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 16px;
  max-height: 400px;
  overflow-y: auto;
  padding: 8px;

  @media (max-width: 768px) {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 12px;
    max-height: 300px;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 2px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 2px;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

.material-item {
  position: relative;
  border-radius: $border-radius-small;
  overflow: hidden;
  box-shadow: $shadow-base;
  border: 2px solid transparent;
  transition: all 0.3s ease;
  cursor: pointer;
  background: white;

  &:hover {
    transform: translateY(-2px);
    box-shadow: $shadow-light;
    border-color: $primary-border;
  }

  &.material-selected {
    border-color: $primary-color;
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }
}

.material-image-container {
  position: relative;
  width: 100%;
  height: 120px;
  overflow: hidden;

  @media (max-width: 768px) {
    height: 100px;
  }
}

.material-image {
  width: 100%;
  height: 100%;
  transition: all 0.3s ease;

  .material-item:hover & {
    transform: scale(1.05);
  }
}

.image-error {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: $text-placeholder;
  font-size: 24px;
}

.selected-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 82, 204, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  animation: fadeIn 0.3s ease;

  @keyframes fadeIn {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
}

.check-icon {
  font-size: 32px;
  font-weight: bold;

  @media (max-width: 768px) {
    font-size: 24px;
  }
}

.material-name {
  padding: 8px 12px;
  font-size: 12px;
  color: $text-regular;
  background: white;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 6px 8px;
    font-size: 11px;
  }
}

.material-pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;

  @media (max-width: 768px) {
    margin-top: 16px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 4px;
      margin: 0 2px;
      transition: all 0.3s ease;
      font-size: 12px;
      min-width: 28px;
      height: 28px;
      line-height: 26px;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 4px;
      transition: all 0.3s ease;
      font-size: 12px;
      min-width: 28px;
      height: 28px;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }
  }
}

/* 上传组件 */
.upload-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.enhanced-upload {
  :deep(.el-upload) {
    border-radius: $border-radius-small;
    transition: all 0.3s ease;

    &:hover {
      border-color: $primary-color;
    }
  }
}

.upload-tip {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: $text-secondary;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

.tip-icon {
  color: $primary-color;
  font-size: 14px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 弹窗底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: #fafbff;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column-reverse;
    gap: 8px;
  }
}

.submit-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    transform: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    padding: 12px 20px;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.cancel-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    padding: 12px 20px;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #374151;
  }

  .dialog-footer {
    background: #4b5563;
    border-color: #6b7280;
  }

  .section-title {
    color: #f9fafb;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #d1d5db;
      }
    }
  }

  .selected-tags {
    background: #4b5563;
    border-color: #6b7280;
  }

  .custom-tag {
    background: rgba(96, 165, 250, 0.1);
    border-color: rgba(96, 165, 250, 0.3);
    color: #60a5fa;

    &:hover {
      background: #60a5fa;
      color: white;
    }
  }

  .type-option {
    background: #4b5563;
    border-color: #6b7280;
    color: #d1d5db;

    &:hover {
      background: rgba(96, 165, 250, 0.1);
      border-color: rgba(96, 165, 250, 0.3);
      color: #60a5fa;
    }

    &.type-active {
      background: #60a5fa;
      border-color: #60a5fa;
      color: white;
    }
  }

  .material-section {
    background: #4b5563;
    border-color: #6b7280;
  }

  .material-title {
    color: #f9fafb;
  }

  .material-count {
    background: #374151;
    border-color: #6b7280;
    color: #d1d5db;
  }

  .material-item {
    background: #374151;

    &:hover {
      border-color: rgba(96, 165, 250, 0.3);
    }

    &.material-selected {
      border-color: #60a5fa;
    }
  }

  .material-name {
    background: #374151;
    color: #d1d5db;
    border-color: #6b7280;
  }

  .selected-overlay {
    background: rgba(96, 165, 250, 0.8);
  }

  .image-error {
    background: #4b5563;
    color: #9ca3af;
  }

  .upload-tip {
    color: #d1d5db;
  }
}
</style>
